#include <QXmlStreamWriter>
#include <QDateTime>
#include <QBuffer>
#include "XmlHelper.h"
#include "scImpl.h"

#define FCT "XmlHelper::domain2DomainText"
QString XmlHelper::domain2DomainText(DataDomain domain)
{
	switch(domain)
	{
	case DOMAIN_ACTION:
		return QString("domain_action");
	case DOMAIN_DATA:
		return QString("domain_data");
	case DOMAIN_DATATL:
		return QString("domain_datatimelined");
	case DOMAIN_DATAVS:
		return QString("domain_dataversioned");
	case DOMAIN_METADATA:
		return QString("domain_metadata");
	default:
		return QString("nodomain");
	}
}

#undef FCT
#define FCT "XmlHelper::domainText2Domain"
DataDomain XmlHelper::domainText2Domain(QString domainText)
{
	if(domainText=="domain_action")
		return DOMAIN_ACTION;
	else if(domainText=="domain_data")
		return DOMAIN_DATA;
	else if(domainText=="domain_datatimelined")
		return DOMAIN_DATATL;
	else if(domainText=="domain_dataversioned")
		return DOMAIN_DATAVS;
	else if(domainText=="domain_metadata")
		return DOMAIN_METADATA;
	return DOMAIN_UNDEF;
}

#undef FCT
#define FCT "XmlHelper::requestType2requestTypeText"
QString XmlHelper::requestType2requestTypeText(RequestType requestType)
{
	switch(requestType)
	{
	case REQU_READ:
		return QString("read");
	case REQU_WRITE:
		return QString("write");
	case REQU_ACTION:
		return QString("action");
	default:
		return QString("norequesttype");
	}
}

#undef FCT
#define FCT "XmlHelper::requstTypeText2requestType"
RequestType XmlHelper::requestTypeText2requestType(QString requestTypeText)
{
	if(requestTypeText=="read")
		return REQU_READ;
	else if(requestTypeText=="write")
		return REQU_WRITE;
	else if(requestTypeText=="action")
		return REQU_ACTION;
	return REQU_UNDEF;
}

#undef FCT
#define FCT "XmlHelper::scopeType2scopeTypeText"
QString XmlHelper::scopeType2scopeTypeText(ScopeType scopeType)
{
	switch(scopeType)
	{
	case SCOPE_TAKEALL:
		return QString("take_all");
	case SCOPE_IK:
		return QString("take_IK");
	case SCOPE_IKTS:
		return QString("take_IK_TS");
	case SCOPE_PK:
		return QString("take_PK");
	case SCOPE_TS:
		return QString("take_TS");
	default:
		return QString("noscopetype");
	}
}

#undef FCT
#define FCT "XmlHelper::scopeTypeText2scopeType"
ScopeType XmlHelper::scopeTypeText2scopeType(QString scopeTypeText)
{
	if(scopeTypeText=="take_all")
		return SCOPE_TAKEALL;
	else if(scopeTypeText=="take_IK")
		return SCOPE_IK;
	else if(scopeTypeText=="take_IK_TS")
		return SCOPE_IKTS;
	else if(scopeTypeText=="take_PK")
		return SCOPE_PK;
	else if(scopeTypeText=="take_TS")
		return SCOPE_TS;
	return SCOPE_UNDEF;
}

#undef FCT
#define FCT "XmlHelper::createXmlDoc"
QXmlStreamWriter * XmlHelper::createXmlDoc(QBuffer * buffer)
{
	QXmlStreamWriter * wr(new QXmlStreamWriter(buffer));
	wr->setAutoFormatting(true);
	wr->setAutoFormattingIndent(2);
	wr->writeStartDocument(QString("1.0"));
	wr->writeStartElement(QString("sybil-pm"));
	wr->writeAttribute(QString("xmlns"), QString("http://sybil-pm.org"));
	wr->writeAttribute(QString("xmlns:xsi"), QString("http://www.w3.org/2001/XMLSchema-instance"));
	wr->writeAttribute(QString("xsi:schemaLocation"), QString("http://sybil-pm.org sybil-pm.xsd"));
	wr->writeAttribute(QString("timestamp_UTC"), QDateTime::currentDateTime().toUTC().toString());
	return wr;
}

#undef FCT
#define FCT "XmlHelper::finishXmlDoc"
void XmlHelper::finishXmlDoc(QXmlStreamWriter * xmlWriter)
{
	xmlWriter->writeEndElement(); // sybil-pm
	xmlWriter->writeEndDocument();
	delete xmlWriter;
}
